package cn.xhy.module.purchase.service.apply;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.xhy.module.purchase.dal.dataobject.materialdemand.ProjectOrderDO;
import cn.xhy.module.purchase.dal.mysql.materialdemand.ProjectOrderMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import cn.xhy.module.purchase.controller.admin.apply .vo.*;

import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.xhy.module.purchase.enums.ErrorCodeConstants.*;

/**
 * 项目物资申请单 Service 实现类
 *
 * @author xhy
 */
@Service
@Validated
public class ProjectOrderServiceImpl implements ProjectOrderService {

    @Resource
    private ProjectOrderMapper projectOrderMapper;

    @Override
    public Long createProjectOrder(ProjectOrderSaveReqVO createReqVO) {
        // 插入
        ProjectOrderDO projectOrder = BeanUtils.toBean(createReqVO, ProjectOrderDO.class);
        projectOrderMapper.insert(projectOrder);
        // 返回
        return projectOrder.getId();
    }

    @Override
    public void updateProjectOrder(ProjectOrderSaveReqVO updateReqVO) {
        // 校验存在
        validateProjectOrderExists(updateReqVO.getId());
        // 更新
        ProjectOrderDO updateObj = BeanUtils.toBean(updateReqVO, ProjectOrderDO.class);
        projectOrderMapper.updateById(updateObj);
    }

    @Override
    public void deleteProjectOrder(Long id) {
        // 校验存在
        validateProjectOrderExists(id);
        // 删除
        projectOrderMapper.deleteById(id);
    }

    private void validateProjectOrderExists(Long id) {
        if (projectOrderMapper.selectById(id) == null) {
            throw exception(PROJECT_ORDER_NOT_EXISTS);
        }
    }

    @Override
    public ProjectOrderDO getProjectOrder(Long id) {
        return projectOrderMapper.selectById(id);
    }

    @Override
    public PageResult<ProjectOrderDO> getProjectOrderPage(ProjectOrderPageReqVO pageReqVO) {
        return projectOrderMapper.selectPage(pageReqVO);
    }

}
